package com.cloudkinto.controller.order.allocate;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.allocate.*;
import com.cloudkinto.bo.stock.ProductStockBySkuQueryBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.allocate.AllocatePlanService;
import com.cloudkinto.service.order.outbound.OutboundImportExportService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.order.allocate.AllocatePlanBatchVo;
import com.cloudkinto.vo.order.allocate.AllocatePlanLockStockVo;
import com.cloudkinto.vo.order.allocate.AllocateProductListVo;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 调拨发货计划 前端控制器
 * </p>
 *
 * @author long
 * @since 2025-03-11
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Oms调拨发货计划模块")
@RequestMapping("/api/admin/oms/allocatePlan")
public class AllocatePlanOmsController extends BaseController {

    private final AllocatePlanService allocatePlanService;
    private final OutboundImportExportService outboundImportExportService;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "调拨发货计划新增", notes = "调拨发货计划新增")
    public SingleResult add(@RequestBody @Validated AllocatePlanAddBo req) {
        return SingleResult.success("新增成功", allocatePlanService.add(req, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PutMapping("/update")
    @ApiOperation(value = "调拨发货计划修改", notes = "调拨发货计划修改")
    public SingleResult update(@RequestBody @Validated AllocatePlanUpdateBo req) {
        allocatePlanService.update(req, getCurrentUserId(), getUserName());
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @PutMapping("/markingUrgency")
    @ApiModelProperty(value = "标记加急", notes = "标记加急")
    public SingleResult markingUrgency(@RequestBody @Validated AllocateUrgencyOperateBo bo) {
        allocatePlanService.markingUrgency(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("标记成功");
    }

    @UserSessionRequired
    @PutMapping("/cancelUrgency")
    @ApiModelProperty(value = "取消加急", notes = "取消加急")
    public SingleResult cancelUrgency(@RequestBody @Validated AllocateUrgencyOperateBo bo) {
        allocatePlanService.cancelUrgency(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("取消加急");
    }

    @UserSessionRequired
    @PutMapping("/removePlanItem")
    @ApiModelProperty(value = "移除调拨发货计划产品", notes = "移除调拨发货计划产品")
    public SingleResult removePlanItem(@RequestBody @Validated AllocatePlanRemoveItemBo bo) {
        allocatePlanService.removePlanItem(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("移除成功");
    }

    @UserSessionRequired
    @PostMapping("/platformProductListPage")
    @ApiOperation(value = "调拨fba发货计划查询产品列表（分页）", notes = "调拨fba发货计划查询产品列表（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = AllocateProductListVo.class))
    public PageResult<AllocateProductListVo> platformProductListPage(@RequestBody @Validated AllocatePlanProductQueryBo bo) {
        return allocatePlanService.platformProductListPage(bo);
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "调拨发货计划根据id删除", notes = "调拨发货计划根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        allocatePlanService.delete(id, getCurrentUserId(), getUserName());
        return SingleResult.success("删除成功");
    }

    @UserSessionRequired
    @PostMapping("/detail")
    @ApiOperation(value = "调拨发货计划根据id查看详情", notes = "调拨发货计划根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = AllocatePlanBatchVo.class))
    public SingleResult<AllocatePlanBatchVo> detail(@RequestBody @Validated AllocatePlanDetailBo detail) {
        ProductStockBySkuQueryBo productStockBySkuQueryBo = new ProductStockBySkuQueryBo();
        productStockBySkuQueryBo.setStockAttribute(0);
        productStockBySkuQueryBo.setCompanyIdList(Collections.singletonList(getCompanyId()));
        productStockBySkuQueryBo.setSource("oms");
        productStockBySkuQueryBo.setTenantId(getTenantId());
        productStockBySkuQueryBo.setUserId(getCurrentUserId());
        return SingleResult.success("查询成功", allocatePlanService.detail(detail, productStockBySkuQueryBo));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "调拨发货计划查询初始化", notes = "调拨发货计划查询初始化")
    public SingleResult<Map<String, Object>> pageInit() {
        return allocatePlanService.pageInit(getCurrentUserId(), getTenantId(), getCompanyId(), "oms");
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "调拨批量发货计划查询（分页）", notes = "调拨批量发货计划查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = AllocatePlanBatchVo.class))
    public PageResult<AllocatePlanBatchVo> listPage(@RequestBody @Validated AllocatePlanQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        return allocatePlanService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusSta")
    @ApiOperation(value = "调拨批量发货计划查询分页统计", notes = "调拨批量发货计划查询分页统计")
    public SingleResult<List<Map<String, Object>>> listPageStatusSta(@RequestBody @Validated AllocatePlanQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        return allocatePlanService.getListPageStatusSta(bo);
    }

    @UserSessionRequired
    @PostMapping("/lockStock/{batchId}")
    @ApiModelProperty(value = "锁定库存", notes = "锁定库存")
    @ApiResponses(@ApiResponse(code = 200, message = "锁定成功", response = AllocatePlanLockStockVo.class))
    public SingleResult lockStock(@PathVariable("batchId") Long batchId) {
        return SingleResult.success("锁定成功", allocatePlanService.lockStock(batchId, getTenantId(), getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PostMapping("/submitOrderToWms/{batchId}")
    @ApiModelProperty(value = "提交订单到wms", notes = "提交订单到wms")
    public SingleResult submitOrderToWms(@PathVariable("batchId") Long batchId) {
        allocatePlanService.submitOrderToWms(batchId, getCurrentUserId(), getUserName());
        return SingleResult.success("提交成功");
    }

    @UserSessionRequired
    @PostMapping("/unLockStock/{batchId}")
    @ApiModelProperty(value = "解锁库存", notes = "解锁库存")
    public SingleResult unLockStock(@PathVariable("batchId") Long batchId) {
        allocatePlanService.unLockStock(batchId, getTenantId(), getCurrentUserId(), getUserName());
        return SingleResult.success("解锁成功");
    }

    @UserSessionRequired
    @PostMapping("/allocateExportBy3rdWh")
    @ApiOperation(value = "调拨发货计划导出三方仓", notes = "调拨发货计划导出三方仓")
    public void allocateExportBy3rdWh(@RequestBody @Validated AllocatePlanQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        outboundImportExportService.allocateExportBy3rdWh(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/allocateExportByFba")
    @ApiOperation(value = "调拨发货计划导出fba", notes = "调拨发货计划导出fba")
    public void allocateExportByFba(@RequestBody @Validated AllocatePlanQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyId(getCompanyId());
        outboundImportExportService.allocateExportByFba(bo, response);
    }

    @UserSessionRequired
    @GetMapping("/getFbaAllotOutboundImportTemplate")
    @ApiOperation(value = "获取Fba调拨出库导入模板", notes = "获取Fba调拨出库导入模板")
    public void getFbaAllotOutboundImportTemplate(HttpServletResponse response) throws IOException {
        outboundImportExportService.getFbaAllotOutboundImportTemplate(response, getTenantId(), getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/get3rdWhAllotOutboundImportTemplate")
    @ApiOperation(value = "获取三方仓调拨出库导入模板", notes = "获取三方仓调拨出库导入模板")
    public void get3rdWhAllotOutboundImportTemplate(HttpServletResponse response) throws IOException {
        outboundImportExportService.get3rdWhAllotOutboundImportTemplate(response, getTenantId(), getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("/allocateImportByFba")
    @ApiOperation(value = "调拨发货计划导入fba", notes = "调拨发货计划导入fba")
    @ApiResponses(@ApiResponse(code = 200, message = "导入成功", response = CommonBatchOperationVo.class))
    public SingleResult allocateImportByFba(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importFbaAllocateOutbound(file, getTenantId(), getCurrentUserId(), getUserName(), getCompanyId()));
    }

    @UserSessionRequired
    @PostMapping("/allocateImportBy3rdWh")
    @ApiOperation(value = "调拨发货计划导入三方仓", notes = "调拨发货计划导入三方仓")
    @ApiResponses(@ApiResponse(code = 200, message = "导入成功", response = CommonBatchOperationVo.class))
    public SingleResult allocateImportBy3rdWh(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.import3rdWhAllocateOutbound(file, getTenantId(), getCurrentUserId(), getUserName(), getCompanyId()));
    }
}